package cn.wolfcode.luowowo.article.job;

import cn.wolfcode.luowowo.api.cache.domain.StrategyStatisVO;
import cn.wolfcode.luowowo.api.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.article.domain.PersistenceStrategyStatisVO;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

//@Component
public class RedisDataPersistenceJob {
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IStrategyDetailService strategyDetailService;

    @Scheduled(cron = "0/10 * * * * ?")
    public void dowork(){
        System.out.println("---------------------");
        //从redis中将数据转移到mybatis中
        List<StrategyStatisVO> vos = strategyStatisRedisService.getAllStatisVos();
        //遍历vos,更新mybatis中的数据
        for (StrategyStatisVO vo : vos) {
            PersistenceStrategyStatisVO psvo = new PersistenceStrategyStatisVO();
            try {
                BeanUtils.copyProperties(psvo,vo);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            strategyDetailService.updateStatisData(psvo);
        }
    }
}
